package no.ntnu.idi.deid.searchresultunits;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

public class CategoryTag {
	private String mDescription = "";
	private String mCode = "";
	private Tag mTag;
	
	public CategoryTag(Tag tag) {
		mTag =  tag;
		mDescription = "";
		mCode = "";
	}
	
	public CategoryTag(Tag tag, String code, String description) {
		mTag =  tag;
		setValues(code, description);
	}
	
	public void setValues(String code, String description) {
		mDescription = description;
		mCode = code;
	}
		
	public String getCode(){
		return mCode;
	}
	
	public Tag getTag() {
		return mTag;
	}
	
	public String getDescription(){
		return mDescription;
	}
	
	public boolean isSensitive() {
		return mTag.isSensitive();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Tag) {
			if(obj.equals(getTag())) {
				return true;
			}
		}
		if(obj instanceof CategoryTag) {
			 final CategoryTag other = (CategoryTag) obj;
		        return new EqualsBuilder()
		            .append(mTag, other.mTag)
		            .append(mDescription, other.mDescription)
		            .append(mCode, other.mCode)
		            .isEquals();
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		if(mDescription == null || 
		   mCode == null ||
		   mDescription.equals("") ||
		   mCode.equals("")) {
			return mTag.hashCode();
		}
		else {
			return new HashCodeBuilder(13,31)
			.append(mTag)
			.append(mDescription)
			.append(mCode)
			.toHashCode();
		}
		
	}

	public static CategoryTag getCategoryTagByString(String str){
		Tag tag = Tag.getCategoryTagByString(str);
		if(tag != null) {
			return new CategoryTag(tag);
		}
		return null;
	}
	
	@Override
	public String toString() {
		return mTag.toString();
	}
	
}
